
import axios from "axios";

import { createBrowserHistory } from 'history'
const service = axios.create({
    baseURL: process.env.REACT_APP_BASE_URL,
    timeout: 10 * 1000
})

var history = createBrowserHistory()

//设置请求拦截器
service.interceptors.request.use((config) => {
    config.headers['Authorization'] = 'Bearer ' + localStorage.getItem('token');
    return config

}, (err) => {
    return Promise.reject(err)
})

//设置响应拦截器
service.interceptors.response.use((res) => {
    return res

}, (err) => {
    if (err.response.status == 401) {
        alert("登录过期，请重新登录！")
        history.push('/login')
        localStorage.removeItem("token")
        localStorage.removeItem("userinfo")


    } else if (err.response.status == 404) {
        alert("资源路径有误！")
    } else if (err.response.status == 500) {
        alert("服务器内部错误！")
    }

    return Promise.reject(err)
})

export default service